'''
Created on 06/08/2010

@author: eh01
'''
from datastore.spaces import Spaces
from datastore.uri import Uri
from services.logger_service import LoggerService
import unittest
from datastore.database import DataStore
from datastore.resource import Resource
from runtime.nodelogger import NodeLogger


class LoggerServiceTest(unittest.TestCase):


    def setUp(self):
        pass
    
    def tearDown(self):
        pass

    def save_log_entry(self, message, commit):
        return
    
    def test_cb_start(self):
        spaces = Spaces()
        logger_service = LoggerService(spaces, uri=Uri())
        logger_service.cb_start(None, None, None)
        self.assertEqual(logger_service.cb_save_logs, spaces.process[Uri('internal:base/services/logger/logs/')][0])
        self.assertEqual(logger_service.cb_get_logs, spaces.process[Uri('internal:base/services/logger/logs')][0])
        
    def test_save_log_entry(self):
        NodeLogger.load()
        ds = DataStore(filename=":memory:")
        logger_service = LoggerService(Spaces(), uri=Uri())
        logger_service.datastore = ds
        messages = ["Message1", "Message2", "Message3"]
        for message in messages:
            logger_service.save_log_entry(message, commit=True)
        message_list = ds.get_n_logs(n=3)
        self.assertEqual(messages[2], message_list[0][0])
        self.assertEqual(messages[1], message_list[1][0])
        self.assertEqual(messages[0], message_list[2][0])

    def test_cb_save_logs_put_and_cb_get_logs_get(self):
        NodeLogger.load()
        res = Resource(type="term:base/services/logger/logs")
        log_entries = [("Message1", None), ("Message2", None), ("Message3", None)]
        res.log_entries = log_entries
        ds = DataStore(filename=":memory:")
        logger_service = LoggerService(Spaces(), uri=Uri())
        logger_service.datastore = ds
        logger_service.cb_save_logs(None, Spaces.ACTION_PUT, res)
        rs = logger_service.cb_get_logs(None, Spaces.ACTION_GET, None)
        self.assertEqual(Uri("term:base/services/logger/logs"), rs.get_type())
        self.assertTrue(rs.has_var('log_entries'))
        self.assertEqual(log_entries[2][0], rs.log_entries[0][0])
        self.assertEqual(log_entries[1][0], rs.log_entries[1][0])
        self.assertEqual(log_entries[0][0], rs.log_entries[2][0])

    def test_cb_save_logs_exists(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(False, logger_service.cb_save_logs(None, Spaces.ACTION_EXISTS, None))
                    
    def test_cb_save_logs_get(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_save_logs(None, Spaces.ACTION_GET, None))
        
    def test_cb_save_logs_update(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_save_logs(None, Spaces.ACTION_UPDATE, None))
        
    def test_cb_save_logs_delete(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_save_logs(None, Spaces.ACTION_DELETE, None))

    def test_cb_save_logs_remove(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_save_logs(None, Spaces.ACTION_REMOVE, None))  
        
    def test_cb_get_logs_exists(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(True, logger_service.cb_get_logs(None, Spaces.ACTION_EXISTS, None))
                    
    def test_cb_get_logs_put(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_get_logs(None, Spaces.ACTION_PUT, None))
        
    def test_cb_get_logs_update(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_get_logs(None, Spaces.ACTION_UPDATE, None))
        
    def test_cb_get_logs_delete(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_get_logs(None, Spaces.ACTION_DELETE, None))

    def test_cb_get_logs_remove(self):
        NodeLogger.load()
        logger_service = LoggerService(Spaces(), uri=Uri())
        self.assertEqual(None, logger_service.cb_get_logs(None, Spaces.ACTION_REMOVE, None))                        
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_cb_start']
    unittest.main()